<?php

/**
 * 台湾CMS系统 twCMS
 *
 * (c) cighsen02 <xiayu@959602.com>
 *
 * Create at：2025.05.07
*/

namespace App\Exceptions;

use Exception;
use Illuminate\Http\JsonResponse;

/**
 * 异常json渲染
 */
class JsonResponseException extends Exception
{
    private array $errors;

    public function __construct(string $message = '', array $errors = [], int $status = 500)
    {
        $this->errors = $errors;

        parent::__construct($message, $status);
    }

    /**
     * Summary of render
     *
     * @return JsonResponse
     */
    public function render(): JsonResponse
    {
        $response = [
            'status'    => false,
            'message'   => $this->getMessage()
        ];

        if(!empty($this->errors)){
            $response += ['errors' => $this->errors];
        }

        return new JsonResponse($response, $this->getCode());
    }
}
